/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.testis.gui.mvc.controller.testbed;

import org.testis.gui.mvc.controller.AbstractController;
import org.testis.gui.mvc.controller.TestBedControllerList;
import org.testis.gui.mvc.model.testbed.DutModel;
import org.testis.gui.mvc.view.testbed.DutView;
import org.testis.orm.testbed.refined.DeviceDefinitionRefined;
import org.testis.orm.testbed.refined.DeviceFirmwareRefined;
import org.testis.orm.testbed.refined.DutRefined;
import org.testis.orm.testbed.refined.SerialConnectionRefined;
import org.testis.orm.testbed.refined.TestBedItem;
import org.testis.orm.testbed.refined.TestPcRefined;
import org.testis.orm.testbed.datahandlers.DutDataHandler;
import org.testis.orm.testbed.refined.DutFactory;
import org.testis.utils.tree.TbedMutableTreeNode;
import java.util.HashMap;

/**
 *
 * @author selcuk
 */
public class DutController extends AbstractController {

    public static final String VISIBLE_PROPERTY = "Visibility";
    public static final String ERROR_PROPERTY = "LastError";
    public static final String DUT_REFINED_PROPERTY = "DutRefined";
    public static final String DUT_ID_PROPERTY = "DutId";
    public static final String TEST_PC_ID_PROPERTY = "TestPcId";
    public static final String DEVICE_ID_PROPERTY = "DeviceDefinitionId";
    public static final String FIRMWARE_ID_PROPERTY = "ActiveFirmwareId";
    public static final String NAME_PROPERTY = "Name";
    public static final String IP_PROPERTY = "TestBedIp";
    public static final String SERIAL_ID_PROPERTY = "SerialId";
    public static final String DSLAM_ID_PROPERTY = "DslamId";
    public static final String SERIAL_ACTIVE_PROPERTY = "IsSerialActive";
    private TbedMutableTreeNode m_selectedNode = null;

    public DutController() {
        super();
    }

    private void initialize(boolean editMode) {
        DutModel model = new DutModel();
        DutView view = new DutView(
                TestBedControllerList.newInstance().getParentFrame(),
                true, editMode);
        super.addModel(model);
        super.addView(view);
        view.pack();
    }

    public void operationCloseWindow() {
        super.cleanAll();
    }

    public void operationEditDut(TbedMutableTreeNode node) {
        if (node.getUserObject() instanceof DutRefined) {
            m_selectedNode = node;
            DutRefined dutRefined = getSelectedDut();
            if (dutRefined == null) {
                System.out.println("Not found design part for DutRefined");
                return;
            }
            initialize(true);


            setModelProperty(DUT_REFINED_PROPERTY, dutRefined);
            setModelProperty(DUT_ID_PROPERTY, dutRefined.getId());

            TestPcRefined testPc = dutRefined.getPc();
            if (testPc != null) {
                setModelProperty(TEST_PC_ID_PROPERTY, testPc.getId());
            }

            DeviceDefinitionRefined deviceDefinition = dutRefined.getDeviceDefinition();
            if (deviceDefinition != null) {
                setModelProperty(DEVICE_ID_PROPERTY, deviceDefinition.getId());
            }

            DeviceFirmwareRefined deviceFirmware = dutRefined.getActiveFirmware();
            if (deviceFirmware != null) {
                setModelProperty(FIRMWARE_ID_PROPERTY, deviceFirmware.getId());
            }
            setModelProperty(NAME_PROPERTY, dutRefined.getName());
            setModelProperty(IP_PROPERTY, dutRefined.getTestBedIp());

            SerialConnectionRefined serialConnection = dutRefined.getSerialConnection();
            if (serialConnection != null) {
                setModelProperty(SERIAL_ID_PROPERTY, serialConnection.getId());
            }
            setModelProperty(SERIAL_ACTIVE_PROPERTY,
                    dutRefined.getIsSerialActive());

            setModelProperty(VISIBLE_PROPERTY, true);
        }
    }

    public void operationNewDut() {
        initialize(false);
        TestBedItem _testBedItem = TestBedControllerList.newInstance().getTbedTreeController().getSelectedTestBedItem();
        if (_testBedItem != null && _testBedItem instanceof TestPcRefined) {
            TestPcRefined _selectedTestPC = (TestPcRefined) _testBedItem;
            setModelProperty(TEST_PC_ID_PROPERTY, _selectedTestPC.getId());
        }
        setModelProperty(VISIBLE_PROPERTY, true);
    }

    public void operationAddDut(int deviceDefinitionId, int activeFirmwareId, String name,
            String testBedIp, int serialId, int pcId, int dslamId, boolean isSerialActive) {
        DutRefined dutRefined = DutFactory.newInstance().createDut(
                deviceDefinitionId, activeFirmwareId, name, testBedIp, serialId,
                pcId, dslamId, isSerialActive);
        if (dutRefined != null) {
            TestBedControllerList.newInstance().getTbedTreeController().operationAddTestBedItem(
                    dutRefined);
        } else {
            setModelProperty(ERROR_PROPERTY,
                    "Unable to add Dut record");
        }
    }

    public void operationSaveDut(DutRefined refined, HashMap values) {
        Integer id = (Integer) values.get("dutId");
        Integer deviceDefinitionId = (Integer) values.get("deviceId");
        Integer activeFirmwareId = (Integer) values.get("activeFirmwareId");
        String name = (String) values.get("name");
        String testBedIp = (String) values.get("testBedIp");
        Integer serialId = (Integer) values.get("serialId");
        Integer pcId = (Integer) values.get("pcId");
        Integer dslamId = (Integer) values.get("dslamId");
        Boolean isSerialActive = (Boolean) values.get("isSerialActive");
        
        boolean move = (refined.getPc().getId() != pcId);
        if (DutDataHandler.newInstance().saveDut(id,
                deviceDefinitionId, activeFirmwareId, name, testBedIp, serialId,
                pcId, dslamId, isSerialActive)) {
            if (move) { //move node from one to another
                TestBedControllerList.newInstance().getTbedTreeController().operationMoveSelectedTestBedItem(
                        pcId);
            }
        } else {
            setModelProperty(ERROR_PROPERTY, "Unable to edit dut record");
        }
    }

    public void operationDeleteDut(DutRefined refined) {
        if (DutFactory.newInstance().deleteDut(refined)) {
            TestBedControllerList.newInstance().getTbedTreeController().operationRemoveSelectedTestBedItem();
        } else {
            TestBedControllerList.newInstance().getTbedTreeController().operationSetErrorProperty(
                    "Unable to remove DUT record");
        }
    }

    public void operationDeleteSelectedDut() {
        TestBedItem tbedItem = TestBedControllerList.newInstance().getTbedTreeController().getSelectedTestBedItem();
        if (tbedItem != null && tbedItem instanceof DutRefined) {
            DutRefined refined = (DutRefined) tbedItem;
            operationDeleteDut(refined);
        }
    }

    private DutRefined getSelectedDut() {
        DutRefined result = null;
        Object obj = m_selectedNode.getUserObject();
        if (obj != null && obj instanceof DutRefined) {
            result = (DutRefined) obj;
        }
        return result;
    }
}
